﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Clothes.Sales.ModuleData;
using ApplicationLibrary.SqlConnector;
using ApplicationLibrary;

namespace Clothes.Sales.Forms
{
    public partial class CounterSales : ApplicationLibrary.DBManager.DocumentForm
    {
        public CounterSales(string formName)
            : base(formName)
        {
            InitializeComponent();
            this.Text = formName;
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerCounterSale("CounterSaleForm", new RadarCounterSales(), this);

            dbManager.AddMaster(this, CL_CounterSales.Name, true);
            dbManager.AddSlave(CL_CounterSalesDetail.Name, true);
            dbManager.AddRelation("CL_CounterSalesDetail", new iColumn[] { CL_CounterSales.DocumentNo },
                                                        new iColumn[] { CL_CounterSalesDetail.DocumentNo }, true);
            dbManager.AddColumn(CL_CounterSalesDetail.Description);

            rdrCustomer.RadarForm = new Clothes.Masters.ModuleData.RadarMasters();
            rdrCustomer.AttachCounterType(Counters.Masters.Int(), GlobalInfo.CurrentDate, null);
            rdrCustomer.DescriptionControl = txtDescription;

            inv_Item.RadarForm = new Clothes.Items.ModuleData.RadarItems() { EnableAddOnFly = false, MustExistCode = true };
            inv_Item.AttachCodeType(Properties.Resources.CN_Items);
        }

        protected override void OnBindData()
        {
            BindControl(fncInvoiceNo, CL_CounterSales.DocumentNo, NullValue.NotSet);
            BindControl(ctbDate, CL_CounterSales.ReleaseDate);
            BindControl(rdrCustomer, CL_CounterSales.CustID);


            BindCounter(fncInvoiceNo);

            dgwSaleDocDetail.AutoGenerateColumns = false;
            dgwSaleDocDetail.DataSource = dbManager.SlaveBinding("CL_CounterSalesDetail");

            BindColumn(inv_Line, CL_CounterSalesDetail.Line);
            BindColumn(inv_Item, CL_CounterSalesDetail.ItemID);
            BindColumn(inv_Price, CL_CounterSalesDetail.Price);

            BindColumn(inv_Description, CL_CounterSalesDetail.Description);

            inv_Item.DescriptionColumn = CL_CounterSalesDetail.Description;

            //dgwSaleDocDetail.AddColumnValidator(CL_SalesDetail.ItemID, new ExtendedDataGridView.DataGridViewColumnValidatingEventHandler(dgwSaleDocDetail_ItemValidating));
            //dgwSaleDocDetail.AddColumnChangeValue(CL_SalesDetail.ItemID, new ExtendedDataGridView.DataGridViewValueChangeEventHandler(dgwSaleDocDetail_ItemChange));
            //dgwSaleDocDetail.AddColumnChangeValue(CL_SalesDetail.Price, new ExtendedDataGridView.DataGridViewValueChangeEventHandler(dgwSaleDocDetail_Price));
            ////dgwJETemplate.DataGridViewRowEnter += new DataGridViewCellEventHandler(dgwJournalEntries_DataGridViewRowEnter);

            //dgwSaleDocDetail.AddReadOnlyColumn(CL_SalesDetail.Description, true);
            //dgwSaleDocDetail.RowIndex = CL_SalesDetail.Line;
            //BindControl(dgwSaleDocDetail);
            //BindControl(ntbTotal);
        }
    }

    public class dbmanagerCounterSale : DBManager
    {
        public dbmanagerCounterSale(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        {
            ForeignKey = CL_CounterSales.DocumentNo;
        }

        protected override SqlABCommand CreateMasterCommand(ref List<SqlABParameter> dParam)
        {
            return base.CreateMasterCommand(ref dParam);
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sP1 = new SqlABParameter("@p1", CL_CounterSales.DocumentNo);
            sP1.Value = CL_CounterSales.DocumentNo.DefaultValue;
            PList.Add(sP1);
            return PList;
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new CL_CounterSales();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(CL_CounterSales.DocumentNo, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateSlaveParam(string name)
        {
            if (name == CL_CounterSalesDetail.Name)
            {
                List<SqlABParameter> PList = new List<SqlABParameter>();

                SqlABParameter sP1 = new SqlABParameter("@p1", CL_CounterSalesDetail.DocumentNo);
                sP1.Value = CL_CounterSalesDetail.DocumentNo.DefaultValue;
                PList.Add(sP1);

                return PList;
            }
            return null;
        }

        protected override string CreateSlaveQuery(string name)
        {
            if (name == CL_CounterSalesDetail.Name)
            {
                QueryBuilder qB = new QueryBuilder();
                qB.AddSelect();
                qB.AddFrom(CL_CounterSalesDetail.Name);
                qB.AddCompare(CL_CounterSalesDetail.DocumentNo, "@p1");
                qB.AddOrder(CL_CounterSalesDetail.Line);
                return qB.Query;
            }
            return "";
        }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override void dAdapter_RowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                if (e.Row.Table.ToString() == CL_CounterSalesDetail.Name)
                {
                    e.Row[CL_CounterSalesDetail.DocumentNo.Name] = GetColumn<string>(CL_CounterSales.DocumentNo, Pos);
                }
            }
            base.dAdapter_RowUpdating(sender, e);
        }
        protected override void SetParameters(IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }
}
